﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.Design;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using T.ApiResults;
using T.Contract.Domain.T.Contract.Model;
using T.Contract.Domain.T.Contract.Model.Dtos;
using T.Contract.Read.API.Application.Command.ContractCommand;
using T.Contract.Read.API.Server;

namespace T.Contract.Read.API.Controllers
{
    /// <summary>
    /// 读控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]

    public class ContractReadController : ControllerBase
    {
        protected readonly IMediator mediator;
        httpPosst httpspost;
        httpGetline httpGetline;

        public ContractReadController(IMediator mediator, httpPosst httpspost, httpGetline httpGetline)
        {
            this.mediator = mediator;
            this.httpspost = httpspost;
            this.httpGetline = httpGetline;
        }
        /// <summary>
        /// 显示合同
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDto<ContractModel>>> Show([FromQuery] ContractShow command)
        {
            ApiResult<PageDto<ContractModel>> result = new ApiResult<PageDto<ContractModel>>();
            try
            {
                command.CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司Id,
                result.Data = await mediator.Send(command);

            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;

        }

        /// <summary>
        /// 显示通用合同
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDto<ContractModel>>> TShowContract([FromQuery]ContractTShowCommand command)
        {
            ApiResult<PageDto<ContractModel>> result = new ApiResult<PageDto<ContractModel>>();
            try
            {
                command.CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司Id,
                result.Data = await mediator.Send(command);

            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;

        }
        /// <summary>
        /// 显示承运合同
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDto<ContractModel>>> CShowContract([FromQuery] ContractCShowCommand command)
        {
            ApiResult<PageDto<ContractModel>> result = new ApiResult<PageDto<ContractModel>>();
            try
            {
                command.CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司Id,
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;

        }
        /// <summary>
        /// 显示货主合同
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDto<ContractModel>>> HShowContract([FromQuery] ContractHShowCommand command)
        {
            ApiResult<PageDto<ContractModel>> result = new ApiResult<PageDto<ContractModel>>();
            try
            {
                command.CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司Id,
                result.Data = await mediator.Send(command);

            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;

        }


        /// <summary>
        /// 反填合同
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<ContractModel>> BackfillTshowContract([FromQuery]ContractBackfillCommand command)
        {
            ApiResult<ContractModel> result = new ApiResult<ContractModel>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;

        }
        [HttpPost]
        public async Task<IActionResult> Ceshi()
        {

            return  Ok(await httpspost.createpost());
        }
        /// <summary>
        /// 显示线路
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet]
       
        public async Task<IActionResult> Getlines()
        {

            try
            {
                return Ok(await httpGetline.Getline());
            }
            catch (Exception)
            {

                throw;
            }
         }
        /// <summary>
        /// 显示货主合同审批记录
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet]
        public async Task<ApiResult<List<HApproval>>> GetApprovals([FromQuery]ContractListCommand command)
        {
            ApiResult<List<HApproval>> result = new ApiResult<List<HApproval>>();
            try
            {
                command.CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司Id,
                result.Data = await mediator.Send(command);

            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        [HttpGet]
        public async Task<IActionResult> Getuname()
        {

           var res= HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取名称
            return Ok(res);
        }

    }
}
